package com.stefanie.eventlistenerdemo.config;

import com.stefanie.eventlistenerdemo.common.event.EventMulticaster;
import com.stefanie.eventlistenerdemo.common.event.EventMulticasterImpl;
import com.stefanie.eventlistenerdemo.listener.core.EventListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author: stefanie-zy
 * @time: 2024/1/12-15:18
 * @description: 事件配置类
 */
@Configuration
public class EventConfig {

    /**
     * Spring容器注册事件广播器
     *
     * @param eventListeners 事件监听器
     * @return 广播器
     */
    @Bean
    public EventMulticaster eventMulticaster(List<EventListener> eventListeners) {
        EventMulticasterImpl eventMulticaster = new EventMulticasterImpl();
        if (eventListeners != null) {
            eventListeners.forEach(eventMulticaster::addEventListener);
        }
        return eventMulticaster;
    }
}
